package com.mirror.utils.base;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.app.Application;
import android.content.Context;

import com.mirror.llk.view.ViewHandler;
import com.mirror.utils.http.HttpUtils;

public class BaseApplication extends Application {

	/** 应用上下文 */
	private static Context context;

	/** HTTP客户端 */
	private static HttpClient httpClient;

	private static ViewHandler viewHandler;

	@Override
	public void onCreate() {
		super.onCreate();
		context = this;
		initHttpClient();
		viewHandler = new ViewHandler();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	/**
	 * 获取应用上下文
	 * 
	 * @return 应用上下文
	 */
	public static Context getContext() {
		return context;
	}

	/**
	 * 初始化HTTP客户端
	 * 
	 * @return HTTP客户端
	 */
	private static void initHttpClient() {
		// 基础参数设置
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
		HttpProtocolParams.setUseExpectContinue(params, true);

		// 超时设置
		// 从连接池中取连接的超时时间
		ConnManagerParams.setTimeout(params, HttpUtils.HTTP_TIME_OUT);
		// 连接超时
		HttpConnectionParams.setConnectionTimeout(params, HttpUtils.HTTP_CONNECTION_TIME_OUT);
		// 请求超时
		HttpConnectionParams.setSoTimeout(params, HttpUtils.HTTP_SO_TIME_OUT);

		// 设置HttpClient支持HTTP和HTTPS两种模式
		SchemeRegistry schReg = new SchemeRegistry();
		schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

		// 使用线程安全的连接管理来创建HttpClient
		ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schReg);
		httpClient = new DefaultHttpClient(connManager, params);
	}

	/**
	 * 获取字符串资源
	 * 
	 * @param resId
	 *            资源ID
	 * @return 字符串资源
	 */
	public static String getStringById(int resId) {
		return context.getString(resId);
	}

	/**
	 * 获取单例模式的HTTP客户端并同步，防止建立多个HTTP客户端
	 * 
	 * @return HTTP客户端
	 */
	public static synchronized HttpClient getHttpClient() {
		if (httpClient == null) {
			initHttpClient();
		}
		return httpClient;
	}

	/**
	 * 关闭单例模式的HTTP客户端并同步，防止同时关闭HTTP客户端导致异常
	 */
	public static synchronized void shutHttpClient() {
		if (httpClient != null && httpClient.getConnectionManager() != null) {
			httpClient.getConnectionManager().shutdown();
			httpClient = null;
		}
	}

	public static ViewHandler getViewHandler() {
		return viewHandler;
	}

}
